;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname e1.11) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))

(define (f n) 
  (cond ((< n 3) n)
        (else (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3)))))))

;(f 1)
;1
;(f 2)
;2
;(f 3)
;(+ (f 2) (* 2 (f 1)) (* 3 (f 0)))
;(+ 2 (* 2 1) (* 3 0))
;(+ 2 2 0)
;(f 4)
;(+ (f 3) (* 2 (f 2)) (* 3 (f 1)))
;(+ (f 4) (* 2 (f 3)) (* 3 (f 2)))

(define (g n)
  (if (< n 3)
      n
      (g-iter 2 1 0 (- n 2))))
  
(define (g-iter a b c count)
  (if (= count 0) 
      a
      (g-iter (+ a (* 2 b) (* 3 c)) a b (- count 1 ))))

;(g 3)
;(g-iter 2 1 0 1)
;(g-iter 4 2 1 0)
;4
;
;(g 4)
;(g-iter 2 1 0 2)
;(g-iter 4 2 1 1)
;(g-iter 11 4 2 0)
;11
;
;(g 5)
;(g-iter 2 1 0 3)
;(g-iter 4 2 1 2)
;(g-iter 11 4 2 1)
;(g-iter 25 11 4 0)

